package io.gitee.hefren.hhmq.model;

import com.google.common.base.MoreObjects;
import com.google.common.collect.Maps;

import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 消息队列中的消息
 * @Date 2024/7/21
 * @Author lifei
 */
public class HhMessage<M> {

    public static final String OFFSET_KEY = "X-offset";
    private static final AtomicLong idGenerator = new AtomicLong(1);
    // 消息的id
    private Long id;
    // 消息体
    private M body;

    // 消息的 系统属性 -全局
    private Map<String, String> headers = Maps.newHashMap();

    // 消息的 业务属性
    private Map<String, String> properties = Maps.newHashMap();

    public HhMessage() {
    }

    public HhMessage(Long id, M body) {
        this.id = id;
        this.body = body;
    }

    public HhMessage(M body) {
        this.id = idGenerator.getAndIncrement();
        this.body = body;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public M getBody() {
        return body;
    }

    public void setBody(M body) {
        this.body = body;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public Map<String, String> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, String> properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(HhMessage.class)
                .add("id", id)
                .add("body", body)
                .add("headers", headers)
                .add("properties", properties)
                .toString();
    }
}
